package daobeauty.interceptor.returntype.executor.criteria;

import org.hibernate.Criteria;

import daobeauty.dao.Limiting;
import daobeauty.dao.Sorting;
import daobeauty.interceptor.returntype.handler.ReturnTypeHandler;

public class UniqueResultCriteriaExecutionStrategy implements CriteriaExecutionStrategy {
	public static final UniqueResultCriteriaExecutionStrategy INSTANCE = new UniqueResultCriteriaExecutionStrategy();
	
	public Object execute(ReturnTypeHandler returnTypeHandler, Criteria criteria, Limiting limiting, Sorting sorting) {
		if (sorting!=null){
			criteria.addOrder(sorting.getOrder());
		}
		criteria.setMaxResults(1);
		if (limiting!=null){
			criteria.setFirstResult(limiting.getOffset());
		}
		return returnTypeHandler.convert(criteria.uniqueResult());
	}
}
